/*
 * $Id: JsonRpcServiceException.java 41065 2007-01-18 23:08:28Z jdamick $
 *
 * Copyright 2006- Revolution Health Group.  All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Revolution Health Group.  (Confidential Information).
 * You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license 
 * agreement you entered into with Revolution Health Group.
 *
 */
package org.javason.exception;

import java.util.Map;

public class JsonRpcServiceException extends RuntimeException {
	public static final String VERSION = "$Rev: 41065 $";
	private static final long serialVersionUID = 1L;
	private Integer _resultCode = 0;

	/**
	 * @param resultCode
	 * @param cause
	 */
	public JsonRpcServiceException(Integer resultCode, Throwable cause) {
		super(getCauseMessage(cause), cause);
		_resultCode = resultCode;
	}

	/**
	 * @param resultCode
	 * @param message
	 * @param cause
	 */
	public JsonRpcServiceException(Integer resultCode, String message, Throwable cause) {
		super(message, cause);
		_resultCode = resultCode;
	}
		
	/**
	 * @param resultCode
	 */
	public JsonRpcServiceException(Integer resultCode) {
		super(resultCode.toString());
		_resultCode = resultCode;
	}
	
	
	/**
	 * 
	 * @param causeToResultCodeMap a map of exceptions to result codes
	 * @param cause the exception thrown
	 * 
	 * Sample:
	 * <code>
	 * Map<Class<? extends Throwable>, Integer> map = new new HashMap<Class<? extends Throwable>, Integer>() {{
	 * 	put(IllegalArgumentException.class, ResultCode.INT_X_151);
	 * }};
	 * </code>
	 */
	public JsonRpcServiceException(Map<Class<? extends Throwable>, Integer> causeToResultCodeMap, Throwable cause) {
		super(getCauseMessage(cause), cause);
		_resultCode = causeToResultCodeMap.get(cause.getClass());
	}
	
	
	/**
	 * @return Returns the resultCode.
	 */
	public Integer getResultCode() {
		return _resultCode;
	}

	
	private static String getCauseMessage(Throwable cause) {
		return (cause.getMessage() != null) ? cause.getMessage() : cause.toString();
	}
}
